package com.ruoyi.rural.controller;

import java.math.BigDecimal;
import java.util.List;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.rural.domain.RuralBenefitAnagement;
import com.ruoyi.rural.service.IRuralBenefitAnagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.rural.domain.RuralBenefitAnagementDonate;
import com.ruoyi.rural.service.IRuralBenefitAnagementDonateService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 救济金捐赠Controller
 * 
 * @author yangming
 * @date 2022-02-14
 */
@RestController
@RequestMapping("/ruralBenefitAnagementDonate")
public class RuralBenefitAnagementDonateController extends BaseController
{
    @Autowired
    private IRuralBenefitAnagementDonateService ruralBenefitAnagementDonateService;

    @Autowired
    private IRuralBenefitAnagementService ruralBenefitAnagementService;

    /**
     * 查询救济金捐赠列表
     */
    @RequiresPermissions("rural:ruralBenefitAnagementDonate:list")
    @GetMapping("/list")
    public TableDataInfo list(RuralBenefitAnagementDonate ruralBenefitAnagementDonate)
    {
        startPage();
        List<RuralBenefitAnagementDonate> list = ruralBenefitAnagementDonateService.selectRuralBenefitAnagementDonateList(ruralBenefitAnagementDonate);
        return getDataTable(list);
    }

    /**
     * 导出救济金捐赠列表
     */
    @RequiresPermissions("rural:ruralBenefitAnagementDonate:export")
    @Log(title = "救济金捐赠", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RuralBenefitAnagementDonate ruralBenefitAnagementDonate)
    {
        List<RuralBenefitAnagementDonate> list = ruralBenefitAnagementDonateService.selectRuralBenefitAnagementDonateList(ruralBenefitAnagementDonate);
        ExcelUtil<RuralBenefitAnagementDonate> util = new ExcelUtil<RuralBenefitAnagementDonate>(RuralBenefitAnagementDonate.class);
        util.exportExcel(response, list, "救济金捐赠数据");
    }

    /**
     * 获取救济金捐赠详细信息
     */
    @RequiresPermissions("rural:ruralBenefitAnagementDonate:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(ruralBenefitAnagementDonateService.selectRuralBenefitAnagementDonateById(id));
    }

    /**
     * 新增救济金捐赠
     */
    @RequiresPermissions("rural:ruralBenefitAnagementDonate:add")
    @Log(title = "救济金捐赠", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RuralBenefitAnagementDonate ruralBenefitAnagementDonate)
    {
        ruralBenefitAnagementDonateService.insertRuralBenefitAnagementDonate(ruralBenefitAnagementDonate);
        RuralBenefitAnagement ruralBenefitAnagement = new RuralBenefitAnagement();
        ruralBenefitAnagement.setBenefitType(ruralBenefitAnagementDonate.getBenefitType());
        List<RuralBenefitAnagement> ruralBenefitAnagements = ruralBenefitAnagementService.selectRuralBenefitAnagementList(ruralBenefitAnagement);
        if (!ruralBenefitAnagements.isEmpty()){
            RuralBenefitAnagement ruralBenefitAnagement1 = ruralBenefitAnagements.get(0);
            BigDecimal totalAmount = new BigDecimal(ruralBenefitAnagement1.getBenefitAmount());
            BigDecimal amount = new BigDecimal(ruralBenefitAnagementDonate.getBenefitAmount());
            ruralBenefitAnagement1.setBenefitAmount(totalAmount.add(amount).toString());
            ruralBenefitAnagementService.updateRuralBenefitAnagement(ruralBenefitAnagement1);
        }else {
            return AjaxResult.error("该救济金类型没有配置银行信息！");
        }
        return AjaxResult.success("添加成功！");
    }

    /**
     * 修改救济金捐赠
     */
    @RequiresPermissions("rural:ruralBenefitAnagementDonate:edit")
    @Log(title = "救济金捐赠", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RuralBenefitAnagementDonate ruralBenefitAnagementDonate)
    {
        return toAjax(ruralBenefitAnagementDonateService.updateRuralBenefitAnagementDonate(ruralBenefitAnagementDonate));
    }

    /**
     * 确认救济金到账
     */
    @RequiresPermissions("rural:ruralBenefitAnagementDonate:edit")
    @Log(title = "救济金捐赠", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/confirm")
    public AjaxResult confirm(@RequestBody RuralBenefitAnagementDonate ruralBenefitAnagementDonate)
    {
        //  修改捐赠状态并更改
        ruralBenefitAnagementDonate.setDonateStatus("2");
        ruralBenefitAnagementDonateService.updateRuralBenefitAnagementDonate(ruralBenefitAnagementDonate);
        //  修改救济金类型总量
        RuralBenefitAnagement ruralBenefitAnagement = new RuralBenefitAnagement();
        ruralBenefitAnagement.setBenefitType(ruralBenefitAnagementDonate.getBenefitType());
        List<RuralBenefitAnagement> ruralBenefitAnagements = ruralBenefitAnagementService.selectRuralBenefitAnagementList(ruralBenefitAnagement);
        // 若不为空则修改总金额,否则
        RuralBenefitAnagement ruralBenefitAnagement1;
        if (!ruralBenefitAnagements.isEmpty()){
            ruralBenefitAnagement1 = ruralBenefitAnagements.get(0);
            BigDecimal totalAmount = new BigDecimal(ruralBenefitAnagement1.getBenefitAmount());
            BigDecimal amount = new BigDecimal(ruralBenefitAnagementDonate.getBenefitAmount());
            ruralBenefitAnagement1.setBenefitAmount(totalAmount.add(amount).toString());
            ruralBenefitAnagementService.updateRuralBenefitAnagement(ruralBenefitAnagement1);
        }else {
            return AjaxResult.error("该救济金类型没有配置银行信息！");
        }
        return AjaxResult.success("确认成功！");
    }

    /**
     * 删除救济金捐赠
     */
    @RequiresPermissions("rural:ruralBenefitAnagementDonate:remove")
    @Log(title = "救济金捐赠", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(ruralBenefitAnagementDonateService.deleteRuralBenefitAnagementDonateByIds(ids));
    }
}
